﻿@namespace BlazorDemo.Pages.CodeSnippets

<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxComboBox-2#cascading-comboboxes">
<CodeSnippetTabPage Text="Razor">@(@"<DxComboBox Data=""@Countries""
            TextFieldName=""@nameof(Country.CountryName)""
            Value=""@CurrentCountry""
            ValueChanged=""@((Country country) => SelectedCountryChanged(country))"">
</DxComboBox>
<DxComboBox Data=""@CurrentCountryCities""
            TextFieldName=""@nameof(City.CityName)""
            @bind-Value=""@CurrentCity"">
</DxComboBox>

@code {
    List<Country> Countries { get; set; } = CountryCity.Countries;
    List<City> CurrentCountryCities { get; set; } = new List<City>();
    Country CurrentCountry { get; set; } = CountryCity.Countries[1];
    City CurrentCity { get; set; } = CountryCity.Cities[4];

    protected override void OnInitialized() {
        base.OnInitialized();
        SelectedCountryChanged(CurrentCountry);
    }

    void SelectedCountryChanged(Country country) {
        CurrentCountry = country;
        CurrentCountryCities = CountryCity.Cities.FindAll(city => city.CountryId == CurrentCountry.Id);
        CurrentCity = CurrentCountryCities[0];
    }
}")
</CodeSnippetTabPage>
<CodeSnippetTabPage Text="Business Objects" Language="csharp">@(@"public class Country {
    public int Id { get; set; }
    public string CountryName { get; set; }
}

public class City {
    public int Id { get; set; }
    public int CountryId { get; set; }
    public string CityName { get; set; }
}

public static class CountryCity {
    private static readonly Lazy<List<Country>> countries = new Lazy<List<Country>>(() => {
        return new List<Country>()
        {
            new Country() { Id = 0, CountryName = ""USA"" },
            new Country() { Id = 1, CountryName = ""Germany"" },
            new Country() { Id = 2, CountryName = ""Japan"" }
        };
    });
    private static readonly Lazy<List<City>> cities = new Lazy<List<City>>(() => {
        return new List<City>() {
            new City() { Id = 0, CountryId = 0, CityName = ""Washington"" },
            new City() { Id = 1, CountryId = 0, CityName = ""New York"" },
            new City() { Id = 2, CountryId = 0, CityName = ""Los Angeles"" },
            new City() { Id = 3, CountryId = 1, CityName = ""Berlin"" },
            new City() { Id = 4, CountryId = 1, CityName = ""Munich"" },
            new City() { Id = 5, CountryId = 1, CityName = ""Hamburg"" },
            new City() { Id = 6, CountryId = 2, CityName = ""Tokyo"" },
            new City() { Id = 7, CountryId = 2, CityName = ""Osaka"" },
            new City() { Id = 8, CountryId = 2, CityName = ""Yokohama"" }
        };
    });
    public static List<Country> Countries { get { return countries.Value; } }
    public static List<City> Cities { get { return cities.Value; } }
}")
</CodeSnippetTabPage>
</CodeSnippetTabbed>
